package portal.action.rest;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import beans.HttpConfig;
import beans.HttpResult;
import engine.BIEngine0_1.AlgorithmOperator;
import net.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import portal.beans.CmdBean;
import portal.cmd.JHCmd;
import portal.cmd.JHostsServices;
import portal.common.MyStringUtils;
import portal.common.ShellExec;
import utils.Log4jInit;
@RestController
@RequestMapping(value="/monitor/")
public class MonitorAction {
	@Autowired
	JHostsServices jHostsServices;
	@Autowired
	JHCmd jhCmd;
	
	@RequestMapping(value="/load/cpu",method=RequestMethod.GET)
	public JSONObject avgCpu(){
		JSONObject result = new JSONObject();
		result.put("result", "failed");
		result.put("message","");
		List<CmdBean>list =jHostsServices.checkNodes();
		if(list==null ||list.size()==0){
			 result.put("message","no node is found");
			 return result;
		}
		int cpuUse=0;
		for(CmdBean b:list){
			cpuUse +=b.getCpu();
			//System.out.println(b.getCpu());
		}
		
		//cpuUse = cpuUse/list.size();
		JSONObject content = new JSONObject();
		content.put("ut", String.format("%.2f", cpuUse*0.01/list.size()));
		result.put("result","success");
		result.put("message",content);
		return result;
	}
	@RequestMapping(value="/load/mem",method=RequestMethod.GET)
	public JSONObject avgMem(){
		List<CmdBean>list =jHostsServices.checkNodes();
		JSONObject result = new JSONObject();
		result.put("result", "failed");
		result.put("message","");
		if(list==null ||list.size()==0)
			return result;
		int memUse=0,memTotal=0;
		for(CmdBean b:list){
			memUse  +=b.getMemU();	
			memTotal+=b.getMemT();
			//System.out.println(b.getMemU()*100/b.getMemT());
		}
		JSONObject content = new JSONObject();
		content.put("ut", String.format("%.2f", memUse*1.0/memTotal));
		result.put("result","success");
		result.put("message",content);
		return result;
	}

	@RequestMapping(value="/stat/jobs",method=RequestMethod.GET)
	public JSONObject jobStatus(){
		JSONObject result = new JSONObject();
		//findJobByStatus("EXIT");
		JSONObject content = new JSONObject();
		int arr[]=findJobByStatus();
		content.put("pend",arr[0]);
		content.put("running", arr[1]);
		content.put("pause",arr[2] );
		result.put("result","success");
		result.put("message",content);
		return result;
	}
	//@RequestMapping(value="/stat/jobs",method=RequestMethod.GET)
	public JSONObject undoneJobs(){
		JSONObject result = new JSONObject();
		//findJobByStatus("EXIT");
		JSONObject content = new JSONObject();
		content.put("pend", findJobByStatus("pend")+findJobByStatus("PEND"));
		content.put("pause", findJobByStatus("pause")+findJobByStatus("PAUSE"));
		content.put("running", findJobByStatus("running")+findJobByStatus("run")+findJobByStatus("RUN"));
		result.put("result","success");
		result.put("message",content);
		return result;
	}
	private String getToken(){
		 HttpResult re=AlgorithmOperator.getToken(null, null);
		 if(re.isResult()){
				return re.getMessage();
		 }
		 return null;
	}
	private int findJobByStatus(String status){
		String token = getToken();
		if(token==null)
			return 0;		
		String re=HttpRequest.sendGet(HttpConfig.appformUrl+HttpConfig.jobsByStatus+status,"token="+token );

		System.out.println(re.length()+"  S:"+re.lastIndexOf("S"));
		Log4jInit.logger.error(re);
		JSONObject jb= JSONObject.parseObject(re);
		if("success".equals(jb.getString("result"))){
			return jb.getJSONArray("data").size();		
		}
		return 0;
	}
	private int[] findJobByStatus(){
		String jobs=ShellExec.command("jjobs -u all");
		int  count[] =new int[3];
		if(jobs==null || jobs.length()<=0)return count;
		String[] jobArr=jobs.split("\n");
		if(jobArr.length<=1)
			return count;
		for(String s :jobArr){
			if(s==jobArr[0])
				continue;
			String[] ele=MyStringUtils.strictSpace(s).split(" ");
			switch(ele[2]){
			case "PEND":count[0]++;break;
			case "RUN":count[1]++;break;
			case "PAUSE":count[2]++;break;
			default:System.out.println(ele[2]);;break;
			}
		}
		return count;
		
	}
}
